package com.jz.community.modulemine.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.bean.WeChatUserInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.constant.HtmlConstant;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.GetLoginWeChatUserTask;
import com.jz.community.basecomm.task.GetUserInfoTask;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.BaseUserUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.bean.CouponNumInfo;
import com.jz.community.modulemine.bean.ShareBean;
import com.jz.community.modulemine.bean.SignDataBean;
import com.jz.community.modulemine.information.bean.SpecialCouponBean;
import com.jz.community.modulemine.information.bean.UploadInfo;
import com.jz.community.modulemine.information.task.AddUserAvatarTask;
import com.jz.community.modulemine.information.task.SpecialCouponTask;
import com.jz.community.modulemine.information.task.UpLoadAvatarTask;
import com.jz.community.modulemine.information.task.UpdateUserAvatarTask;
import com.jz.community.modulemine.information.task.UpdateUserInfoTask;
import com.jz.community.modulemine.information.ui.BindingPhoneActivity;
import com.jz.community.modulemine.information.ui.UserInfoActivity;
import com.jz.community.modulemine.information.view.SpecialCouponDialog;
import com.jz.community.modulemine.integral.ui.MyIntegralActivity;
import com.jz.community.modulemine.message.ui.MessageActivity;
import com.jz.community.modulemine.myCard.info.MyCardInfo;
import com.jz.community.modulemine.myCard.task.GetMyCardListTask;
import com.jz.community.modulemine.myCard.ui.MyCardActivity;
import com.jz.community.modulemine.net.GetCouponNumTask;
import com.jz.community.modulemine.packet.ui.activity.RedPacketActivity;
import com.jz.community.modulemine.presenter.MinePresenter;
import com.jz.community.modulemine.rechargephone.ui.activity.PhoneFareActivity;
import com.jz.community.modulemine.ui.MineView;
import com.jz.community.modulemine.ui.activity.EnterMerchantActivity;
import com.jz.community.modulemine.ui.activity.SettingActivity;
import com.jz.community.modulemine.ui.activity.SignActivity;
import com.jz.community.modulemine.ui.signTask.SignTask;
import com.jz.community.modulemine.ui.view.NoticeOpenDialog;
import com.jz.community.modulemine.ui.view.VerticalScrollView;
import com.jz.community.modulemine.utils.NotificationsUtil;
import com.jz.community.modulemine.wifi.ui.MyWifiActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


@Route(path = RouterIntentConstant.MODULE_MINE_HOME)
public class MineFragment extends BaseMvpFragment<MineView.View, MinePresenter> implements MineView.View {
    @BindView(R2.id.user_my_avatar)
    CircleImageView userMyAvatar;//头像
    @BindView(R2.id.user_my_name)
    TextView userMyName;//昵称
    @BindView(R2.id.user_my_phone)
    TextView userMyPhone;//手机号码
    @BindView(R2.id.user_my_bind_phone_arrow_iv)
    ImageView userMyBindPhoneArrowIv;//绑定手机
    @BindView(R2.id.user_my_info_ll)
    LinearLayout userMyInfoLl;//个人信息
    @BindView(R2.id.user_my_login_ll)
    LinearLayout userMyLoginLl;//去登录
    @BindView(R2.id.user_my_sign_img)
    ImageView userMySignImg;//签到图片
    @BindView(R2.id.user_my_sign_text)
    TextView userMySignText;//签到文字
    @BindView(R2.id.user_my_sign_ll)
    LinearLayout userMySignLl;//签到ll
    @BindView(R2.id.user_my_banner_rl)
    RelativeLayout userMyBannerRl;
    @BindView(R2.id.user_my_money)
    TextView userMyMoney;//钱包
    @BindView(R2.id.user_my_points)
    TextView userMyPoints;//我的积分
    @BindView(R2.id.user_my_rp_iv)
    ImageView userMyRpIv;
    @BindView(R2.id.user_my_rp_arrow)
    ImageView userMyRpArrow;
    @BindView(R2.id.user_my_rp_rl)
    RelativeLayout userMyRpRl;//红包跳转
    @BindView(R2.id.user_my_coupon_iv)
    ImageView userMyCouponIv;
    @BindView(R2.id.user_my_coupon_tips)
    TextView userMyCouponTips;//优惠券数量
    @BindView(R2.id.user_my_coupon_arrow)
    ImageView userMyCouponArrow;
    @BindView(R2.id.user_my_coupon_rl)
    RelativeLayout userMyCouponRl;//优惠券跳转
    @BindView(R2.id.user_my_collect_iv)
    ImageView userMyCollectIv;
    @BindView(R2.id.user_my_collect_arrow)
    ImageView userMyCollectArrow;
    @BindView(R2.id.user_my_collect_rl)
    RelativeLayout userMyCollectRl;//我的收藏跳转
    @BindView(R2.id.user_my_address_iv)
    ImageView userMyAddressIv;
    @BindView(R2.id.user_my_address_arrow)
    ImageView userMyAddressArrow;
    @BindView(R2.id.user_my_address_rl)
    RelativeLayout userMyAddressRl;//我的地址跳转
    @BindView(R2.id.user_my_task_iv)
    ImageView userMyTaskIv;
    @BindView(R2.id.user_my_task_arrow)
    ImageView userMyTaskArrow;
    @BindView(R2.id.user_my_task_rl)
    RelativeLayout userMyTaskRl;//积分任务跳转
    @BindView(R2.id.user_my_point_shop_iv)
    ImageView userMyPointShopIv;
    @BindView(R2.id.user_my_point_shop_arrow)
    ImageView userMyPointShopArrow;
    @BindView(R2.id.user_my_point_shop_rl)
    RelativeLayout userMyPointShopRl;//积分商城跳转
    @BindView(R2.id.user_my_wifi_iv)
    ImageView userMyWifiIv;
    @BindView(R2.id.user_my_wifi_arrow)
    ImageView userMyWifiArrow;
    @BindView(R2.id.user_my_wifi_rl)
    RelativeLayout userMyWifiRl;//wifi跳转
    @BindView(R2.id.user_my_push_iv)
    ImageView userMyPushIv;
    @BindView(R2.id.user_my_push_point)
    ImageView userMyPushPoint;
    @BindView(R2.id.user_my_share_iv)
    ImageView userMyShareIv;
    @BindView(R2.id.user_my_point_share_arrow)
    ImageView userMyPointShareArrow;
    @BindView(R2.id.user_my_share_rl)
    RelativeLayout userMyShareRl;//我的分享跳转
    @BindView(R2.id.user_my_merchant_iv)
    ImageView userMyMerchantIv;
    @BindView(R2.id.user_my_merchant_rl)
    RelativeLayout userMyMerchantRl;//商家入驻
    @BindView(R2.id.user_my_service_iv)
    ImageView userMyServiceIv;
    @BindView(R2.id.user_my_service_rl)
    RelativeLayout userMyServiceRl;//客服
    @BindView(R2.id.user_my_express)
    RelativeLayout userMyExpress;//快递
    @BindView(R2.id.user_my_scroll)
    VerticalScrollView userMyScroll;
    @BindView(R2.id.home_mine_toolbar)
    Toolbar homeMineToolbar;
    @BindView(R2.id.user_my_push_rl)
    ImageView user_my_push_rl;

    private final static int IMAGE_PICKER = 100;

    private int isOpenNotice = 0;

    private UserInfo userInfo;
    private String nickName = "";
    private WeChatUserInfo weChatUserInfo;

    private String shareGiftUrl = "";
    //0 不可分享 1 可以分享
    private int canShareGift = 0;

    public static final int BINDING_PHONE = 205;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_mine_fragment_mine;
    }

    @Override
    protected void initView() {
        mPresenter.setPresenter(this);
        userMyScroll.setPullZoomView(userMyBannerRl);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleLoginType(BaseSpUtils.getInstance().getLoginType(getActivity()));
    }

    /**
     * 兼容滑动状态栏
     */
    private void setMineUserToolbar(boolean isScollState, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isScollState) {
                setImmersionBar(color);
            } else {
                setImmersionBar(color);
            }
        } else {
            setImmersionBar(homeMineToolbar, 0.2f, true);
        }
    }

    @Override
    protected void initVisible() {

        setMineUserToolbar(false, R.color.user_toolbar_color);

        if (BaseUserUtils.getIsLogin(getActivity())) {
            SHelper.vis(userMyInfoLl);
            SHelper.gone(userMyLoginLl);
            handleLoginType(BaseSpUtils.getInstance().getLoginType(getActivity()));
            getIsSign();
            getMoneyDatas();
            //获取消息红点
            mPresenter.unreadCount("member");
            //获取分享有礼的网址
            //mPresenter.initSharegift();
            //优惠券数量
            getCouponNum();
        } else {
            handleUserInfoHide();
        }
    }

    /**
     * 登录处理
     */
    private void handleLoginType(int loginType) {
        if (loginType == 1) {
            getUserInfo(true);
            return;
        }

        if (loginType == 2) {
            getWeChatUserInfoMsg();
            return;
        }
    }

    @Override
    protected MinePresenter createPresenter() {
        return new MinePresenter(this);
    }

    @SuppressLint("NewApi")
    @Override
    protected void addListener() {
        userMyLoginLl.setOnClickListener(v -> ARouter.getInstance().build(RouterIntentConstant.MODULE_AUTHORIZATION_LOGIN).navigation());

        userMyScroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY >= 400) {
                    setMineUserToolbar(true, R.color.white);
                } else {
                    setMineUserToolbar(false, R.color.user_toolbar_color);
                }
            }
        });
    }

    /**
     * 处理用户显示状态
     *
     * @param isUserMsgState
     */
    private void handleUserMsgState(boolean isUserMsgState) {
        if (isUserMsgState) {
            if (!Preconditions.isNullOrEmpty(userInfo.getDefaultIcon())) {
                showUserInfo(userInfo.getDefaultIcon().getIcon(), userInfo.getPhone());
            } else {
                showUserInfo("", userInfo.getPhone());
            }
        } else {
            if (!Preconditions.isNullOrEmpty(userInfo.getDefaultIcon())) {
                showUserInfo(userInfo.getDefaultIcon().getIcon(), userInfo.getPhone());
            } else {
                addUserAvatarInfo(weChatUserInfo.getHeadimgurl());
            }
        }
    }

    /**
     * 用户信息
     */
    private void getUserInfo(boolean isUserMsgState) {
        new GetUserInfoTask(getActivity(), o -> {
            userInfo = (UserInfo) o;
            if (!Preconditions.isNullOrEmpty(userInfo)) {
                if (!Preconditions.isNullOrEmpty(userInfo.getName())) {
                    nickName = userInfo.getName();
                }
                handleUserMsgState(isUserMsgState);
                showUserIntegral(userInfo);
            }
        }).execute();
    }

    /**
     * 微信用户信息
     */
    private void getWeChatUserInfoMsg() {
        new GetLoginWeChatUserTask(getActivity(), o -> {
            weChatUserInfo = (WeChatUserInfo) o;
            if (!Preconditions.isNullOrEmpty(weChatUserInfo)) {
                nickName = weChatUserInfo.getNickname();
                updateUserName(nickName);
                getUserInfo(false);
            }
        }).execute();
    }

    /**
     * 用户卡包
     */
    private void getMoneyDatas() {
        new GetMyCardListTask(getActivity(), o -> {
            MyCardInfo moneyInfo = (MyCardInfo) o;
            if (!Preconditions.isNullOrEmpty(moneyInfo) && !Preconditions.isNullOrEmpty(moneyInfo.get_embedded())) {
                SHelper.vis(userMyMoney);
                userMyMoney.setText(moneyInfo.get_embedded().getContent().size() + "");
            }
        }).execute("1");
    }

    /**
     * 用户是否签到
     */
    private void getIsSign() {
        new SignTask(getActivity(), o -> {
            SignDataBean signInfo = (SignDataBean) o;
            if (!Preconditions.isNullOrEmpty(signInfo) && !Preconditions.isNullOrEmpty(signInfo.getStatus())) {
                if (!RxDataTool.isNullString(signInfo.getStatus())) {
                    //今天是否已签到
                    if (signInfo.getStatus().equals("0")) {
                        //未签到
                        userMySignLl.setVisibility(View.VISIBLE);
                        userMySignText.setText("签到");
                    } else if (signInfo.getStatus().equals("1")) {
                        //已签到
                        userMySignLl.setVisibility(View.VISIBLE);
                        userMySignText.setText("已签到");
                    }
                }
            }
        }).execute();
    }

    /**
     * 上传头像
     *
     * @param filePath
     */
    private void uploadUserAvatar(String filePath) {
        new UpLoadAvatarTask(getActivity(), o -> {
            UploadInfo uploadInfo = (UploadInfo) o;
            if (!Preconditions.isNullOrEmpty(uploadInfo) && !Preconditions.isNullOrEmpty(uploadInfo.getData())) {
                WpToast.l(getActivity(), "上传成功");
                setUserAvatar(uploadInfo.getData().get(0));
                if (!Preconditions.isNullOrEmpty(userInfo.getDefaultIcon())) {
                    updateUserAvatar(uploadInfo.getData().get(0), userInfo.getDefaultIcon().getId(), "1");
                } else {
                    updateUserAvatar(uploadInfo.getData().get(0), "", "2");
                }
            } else {
                WpToast.l(getActivity(), "上传失败");
            }
        }).execute(filePath);
    }

    /**
     * 新增用户头像对象
     *
     * @param filePath
     */
    private void addUserAvatarInfo(String filePath) {
        new AddUserAvatarTask(getActivity(), o -> {
            getUserInfo(true);
        }).execute(filePath);
    }

    /**
     * 更新用户头像
     *
     * @param avatar
     */
    private void updateUserAvatar(String avatar, String avatarId, String updateType) {
        new UpdateUserAvatarTask(getActivity(), new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {

            }
        }).execute(avatar, avatarId, updateType);
    }

    /**
     * 更新用户昵称
     *
     * @param nickName
     */
    private void updateUserName(String nickName) {
        new UpdateUserInfoTask(getActivity(), new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {

            }
        }, false).execute(nickName, "1");
    }

    /**
     * 显示用户中心
     *
     * @param phone
     */
    private void showUserInfo(String avatar, String phone) {
        userMyName.setText(nickName);
        if (Preconditions.isNullOrEmpty(phone)) {
            userMyPhone.setText("绑定手机");
            SHelper.vis(userMyBindPhoneArrowIv);
            if (!BaseSpUtils.getInstance().getIsBindPhone(getActivity())) {
                startActivityForResult(new Intent(getActivity(), BindingPhoneActivity.class), BINDING_PHONE);
                BaseSpUtils.getInstance().setIsBindPhone(getActivity(), true);
            }
        } else {
            BaseSpUtils.getInstance().setIsBindPhone(getActivity(), true);
            userMyPhone.setText(phone);
            SHelper.gone(userMyBindPhoneArrowIv);
        }
        setUserAvatar(avatar);
    }

    /**
     * 用户积分
     *
     * @param userInfo
     */
    private void showUserIntegral(UserInfo userInfo) {
        SHelper.vis(userMyPoints);
        if (!Preconditions.isNullOrEmpty(userInfo.getIntegral())) {
            userMyPoints.setText(userInfo.getIntegral());
            BaseSpUtils.getInstance().setIntegralNum(getActivity(), ConverterUtils.toInt(userInfo.getIntegral()));
        } else {
            userMyPoints.setText("0");
        }
    }

    //优惠券数量
    private void getCouponNum() {
        new GetCouponNumTask(getActivity(), new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                CouponNumInfo couponNumInfo = (CouponNumInfo) o;
                if (!Preconditions.isNullOrEmpty(couponNumInfo) && !Preconditions.isNullOrEmpty(couponNumInfo.getPage())) {
                    if (couponNumInfo.getPage().getTotalElements() > 0) {
                        userMyCouponTips.setText(couponNumInfo.getPage().getTotalElements() + "");
                        SHelper.vis(userMyCouponTips);
                    } else {
                        SHelper.gone(userMyCouponTips);
                    }
                } else {
                    SHelper.gone(userMyCouponTips);
                }
            }
        }).execute("0");
    }

    /**
     * 显示用户头像
     *
     * @param avatar
     */
    private void setUserAvatar(String avatar) {
        if (!Preconditions.isNullOrEmpty(avatar)) {
            if (!getActivity().isFinishing()) {
                BaseImageLoaderUtils.getInstance().loadDefaltImage(getActivity(), userMyAvatar, avatar);
            }
        } else {
            userMyAvatar.setImageResource(R.mipmap.user_new_icon);
        }
    }

    /**
     * 未登录显示UI
     */
    private void handleUserInfoHide() {
        userMyAvatar.setImageResource(R.mipmap.user_new_icon);
        SHelper.vis(userMyLoginLl);
        SHelper.gone(userMyInfoLl, userMyMoney, userMyPoints);
        userMyMoney.setText("0");
        userMyPoints.setText("0");
        userMySignLl.setVisibility(View.VISIBLE);
        userMySignText.setText("签到");
    }

    private void openNoticeDialog() {
        final NoticeOpenDialog dialog = new NoticeOpenDialog(getActivity());

        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.module_mine_dialog_open_notice, null);
        //关闭弹框
        LinearLayout notice_back = (LinearLayout) rootView.findViewById(R.id.notice_back);
        //开启推送
        TextView notice_btn = (TextView) rootView.findViewById(R.id.notice_btn);

        notice_back.setOnClickListener(v -> {
            BaseSpUtils.getInstance().setNoticeStatus(getActivity(), 1);
            dialog.dismiss();
        });

        notice_btn.setOnClickListener(v -> {
            // 进入设置系统应用权限界面
            BaseSpUtils.getInstance().getNoticeStatus(getActivity());

            if (Build.VERSION.SDK_INT >= 26) {
                //android 8.0引导
                Intent intent = new Intent();
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("android.provider.extra.APP_PACKAGE", getActivity().getPackageName());
                startActivity(intent);
            } else if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 26) {
                //android 5.0-7.0
                Intent intent = new Intent();
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", getActivity().getPackageName());
                intent.putExtra("app_uid", getActivity().getApplicationInfo().uid);
                startActivity(intent);
            } else {
                Intent intent = new Intent();
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", getActivity().getPackageName(), null));
                startActivity(intent);
            }
            dialog.dismiss();
        });

        dialog.show();
        dialog.setContentView(rootView);
    }

    @OnClick(R2.id.user_my_coupon_rl)
    public void couponClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_COUPON);
        }
    }

    @OnClick(R2.id.user_my_address_rl)
    public void addressClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            ARouter.getInstance()
                    .build(RouterIntentConstant.MODULE_SHOPPING_ADDRESS_LIST)
                    .withInt("addressFrom", 0)
                    .withInt("sendType", 1)
                    .navigation();
        }
    }

    @OnClick(R2.id.user_my_cw_rl)
    public void userMyCeRlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), BaseX5WebActivity.class)
                    .putExtra("url", HtmlConstant.APP_WAPSERVICE_URL)
                    .putExtra("title", "社区人金融服务"));
        }
    }

    @OnClick(R2.id.user_my_service_rl)
    public void userMyServiceRlClick() {
        startActivity(new Intent(getActivity(), BaseX5WebActivity.class)
                .putExtra("url", Constant.CUSTOM_SERVICE_URL)
                .putExtra("title", "在线客服"));
    }

    @OnClick(R2.id.user_my_setting)
    public void userMySettingClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), SettingActivity.class));
        }
    }

    @OnClick(R2.id.user_my_merchant_rl)
    public void userMyMerchantRlClick() {
        //商家未入驻
        Intent intent_merchant = new Intent(getActivity(), EnterMerchantActivity.class);
        startActivity(intent_merchant);
    }

    @OnClick(R2.id.user_my_info_ll)
    public void userMyInfoLlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            Intent intent = new Intent(getActivity(), UserInfoActivity.class);
            startActivity(intent);
        }
    }

    @OnClick(R2.id.user_my_sign_ll)
    public void userMySignLlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), SignActivity.class));
        }
    }

    @OnClick(R2.id.user_my_money_ll)
    public void userMyMoneyLlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), MyCardActivity.class));
        }
    }

    @OnClick(R2.id.user_my_points_ll)
    public void userMyPointsLlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), MyIntegralActivity.class));
        }
    }

    @OnClick(R2.id.user_my_phone_charge_rl)
    public void userPhoneFareLlClick() {
        //话费充值
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getActivity(), PhoneFareActivity.class));
        }
    }


    @OnClick(R2.id.user_my_rp_rl)
    public void userMyRpLlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            startActivity(new Intent(getContext(), RedPacketActivity.class));
        }
    }

    @OnClick(R2.id.user_my_avatar)
    public void userMyAvatarClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            ImageSelector.builder()
                    .useCamera(true) // 设置是否使用拍照
                    .setSingle(true)  //设置是否单选
                    .setViewImage(true) //是否点击放大图片查看,，默认为true
                    .start(this, IMAGE_PICKER); // 打开相册
        }
    }

    @OnClick(R2.id.user_my_wifi_rl)
    public void userMyWifiRlClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            Intent intent = new Intent(getActivity(), MyWifiActivity.class);
            startActivity(intent);
        }
    }

    @OnClick(R2.id.user_my_share_rl)
    public void shareGifClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            if (canShareGift == 1) {
                startActivity(new Intent(getActivity(), BaseX5WebActivity.class)
                        .putExtra("url", shareGiftUrl)
                        .putExtra("title", "分享有礼"));
            }

        }
    }

    @OnClick(R2.id.user_my_express)
    public void userMyExpressClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.EXPRESS_HOME);
        }
    }

    @OnClick(R2.id.user_all_order_layout)
    public void userAllOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_ORDER_LIST);
        }
    }

    @OnClick(R2.id.user_payment_order_layout)
    public void userPaymentOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_ORDER_PAYMENT);
        }
    }

    @OnClick(R2.id.user_deliver_order_layout)
    public void userDeliverOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_ORDER_DELIVE);
        }
    }

    @OnClick(R2.id.user_receiving_order_layout)
    public void userReceivingOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_ORDER_RECEIVING);
        }
    }

    @OnClick(R2.id.user_evaluated_order_layout)
    public void userEvaluatedOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.MODULE_SHOPPING_ORDER_EVALUTAED);
        }
    }

    @OnClick(R2.id.user_assemble_all_order_layout)
    public void userAssembleAllOrderClick() {
        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            String groupUrl = HtmlConstant.APP_GROUP_URL + BaseSpUtils.getInstance().getAccess_token(getActivity()) + "&type=0";
            ARouter.getInstance().build(RouterIntentConstant.MODULE_SHOPPING_ORDER_HTML_ASSEMBLE)
                    .withString("url", groupUrl)
                    .withString("title", "订单列表")
                    .navigation();
        }
    }

    @OnClick(R2.id.user_my_push_rl)
    public void userMyMessageClick() {
        isOpenNotice = BaseSpUtils.getInstance().getNoticeStatus(getActivity());

        if (BaseUserUtils.showIsLoginDialog(getActivity())) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (NotificationManagerCompat.from(getActivity()).areNotificationsEnabled()) {
                    startActivity(new Intent(getActivity(), MessageActivity.class));
                } else {
                    if (isOpenNotice == 0) {
                        //给用户只提示一次
                        openNoticeDialog();
                    } else {
                        startActivity(new Intent(getActivity(), MessageActivity.class));
                    }
                }
            } else {
                if (NotificationsUtil.isNotificationEnabled()) {
                    startActivity(new Intent(getActivity(), MessageActivity.class));
                } else {
                    if (isOpenNotice == 0) {
                        //给用户只提示一次
                        openNoticeDialog();
                    } else {
                        startActivity(new Intent(getActivity(), MessageActivity.class));
                    }
                }
            }

        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == IMAGE_PICKER) {
            ArrayList<String> images = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT);
            uploadUserAvatar(images.get(0));
        } else if (BINDING_PHONE == requestCode && resultCode == RESULT_OK) {
            getCoupon();
        } else if (BINDING_PHONE == requestCode && resultCode == RESULT_CANCELED) {
            showNormal("没有图片可以更换");
        }
    }


    private void getCoupon() {
        new SpecialCouponTask(getActivity(), o -> {
            SpecialCouponBean specialCouponBean = (SpecialCouponBean) o;
            if (Preconditions.isNullOrEmpty(specialCouponBean)) {
                return;
            }
            SpecialCouponDialog dialog = new SpecialCouponDialog(getActivity());
            dialog.setData(specialCouponBean);
            dialog.show();
        }).execute();
    }


    @Override
    public void showCount(Integer count) {
        if (count > 0) {
            userMyPushPoint.setVisibility(View.VISIBLE);
        } else {
            userMyPushPoint.setVisibility(View.GONE);
        }
    }

    @Override
    public void shareGiftSuccess(ShareBean shareBean) {
        canShareGift = 1;
        shareGiftUrl = shareBean.getData();

    }

    @Override
    public void shareGiftFail(String msg) {
        canShareGift = 0;

    }

    @Override
    public void showError(String s) {

    }
}
